package com.nz.net.hays.www;



import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks, OnClickListener {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    public static int loginFlag=1;
    
    private CharSequence mTitle;
    //private Spinner spinnerLocation;
    private String locationSelected;
//   private ImageButton register,login;
    private Button register,login,search; 
    private Button buttonLocation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
                 
        
       setContentView(R.layout.activity_main);
       
      
       
        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();
        onSectionAttached(0);
        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
        
        
        
        // Navigating through navigation drawer
       
        // Select default
       
        
        
        // Setting Location
        Intent locationActivity = getIntent();
      
        locationSelected=locationActivity.getStringExtra("location");
    
        // setup widget
        setupwidget();
        

        
    
       
    }

   

	

	private void setupwidget() {
		//buttonLocation = (Button)findViewById(R.id.buttonLocation);
		register= (Button)findViewById(R.id.imagebuttonregister);
		login= (Button)findViewById(R.id.imagebuttonlogin);
		search=(Button)findViewById(R.id.imageButtonsearch);
		search.setTextColor(Color.WHITE);
		register.setTextColor(Color.WHITE);
		login.setTextColor(Color.WHITE);
		
	    //   buttonlocation.setText(location1);
		//buttonLocation.setText("Choose Location");
		//location = "Hey Work Now";
		//location=i.getStringExtra("location");
//		/buttonLocation.setBackgroundColor(Color.BLUE);
		
		//buttonLocation.setText(locationSelected);
		//Button buttonLocation = (Button)findViewById(R.id.buttonLocation);
		//buttonLocation.setOnClickListener(this);
		
		register.setOnClickListener(this);
		login.setOnClickListener(this);
		search.setOnClickListener(this);
	}




	

	@Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
        .replace(R.id.container, PlaceholderFragment.newInstance(position+1 ))
       .commit();
       
       
    }

    public void onSectionAttached(int number) {
       
        // navigating
    	//number=number-1;
        switch (number) {
        	case 1:
        	// mTitle = getString(R.string.title_section0);
             
               
        break;
            	case 2:
               // mTitle = getString(R.string.title_section1);
              //  setTitle("Login");
                Intent    intent = new Intent(MainActivity.this, SignIn.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                
                break;
            case 3:
               // mTitle = getString(R.string.title_section2);
                setTitle(mTitle);
                intent = new Intent(getApplicationContext(), SignUp.class);
                startActivity(intent);
                break;
            case 4:
                //mTitle = getString(R.string.title_section3);
            	intent = new Intent(MainActivity.this, SignIn.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            case 5:
                //mTitle = getString(R.string.title_section4);
            	intent = new Intent(MainActivity.this, SignIn.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            case 6:
                //mTitle = getString(R.string.title_section5);
            	intent = new Intent(MainActivity.this, SignIn.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            case 7:
                //mTitle = getString(R.string.title_section6);
            	Toast.makeText(getApplicationContext(), "Coming Soon.....", Toast.LENGTH_LONG).show();
                break;
            case 8:
                //mTitle = getString(R.string.title_section7);
            	openWeb();
            	
                break;
           
        }
    }

    public void openWeb()
    {
    	Intent intent = new Intent();
    	intent.setAction(Intent.ACTION_VIEW);
    	intent.addCategory(Intent.CATEGORY_BROWSABLE);
    	intent.setData(Uri.parse("http://hays.com/terms-and-conditions/index.htm/"));
    	startActivity(intent);
    	Toast.makeText(getApplicationContext(), "Visit our website for details.", Toast.LENGTH_LONG).show();
    }
    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
       // removing side bar - comment code here, goto manifest.xml do addition meta-data tag, go to main.xml in menu do comment code
    	// comment global.xml items, in NavigationDrawerFragment class, go to  onOptionsItemSelected method and comment second if block containing item code 
    	/* int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }*/
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }
// opening a new view 
    
	@Override
	public void onClick(View v) {
		try{
		if (v.getId()==R.id.buttonLocation)
		{
			Intent locationActivity = new Intent(getApplicationContext(),DynamicLocationActivity.class);
			startActivity(locationActivity);
			
		}
		 if(v.getId()==R.id.imagebuttonregister)
		{
			Intent registeractivity = new Intent(getApplicationContext(),SignUp.class);
			startActivity(registeractivity);
			
		}
		 if(v.getId()==R.id.imagebuttonlogin)
		{
			Intent loginactivity = new Intent(getApplicationContext(),SignIn.class);
			startActivity(loginactivity);	
		}
		 if(v.getId()==R.id.imageButtonsearch)
			{
				Intent loginactivity = new Intent(getApplicationContext(),Search.class);
				startActivity(loginactivity);	
			}
		}
		catch(Exception e)
		{
			String msg=e.getMessage();
			Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
			
		}
		
	}

}
